package com.sfm.tour;
import com.sfm.tour.CameraActivity;

import com.google.android.maps.MapActivity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle; 
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.List;



import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class SFMapTagActivity extends MapActivity {
    
    private MapView mapView;
    ArrayList<GeoPoint> pointsList;

    private LocationManager locationManager;
    Geocoder geocoder;
    Location location;
    LocationListener locationListener;
    CountDownTimer locationtimer;
    Movie movieToAdd;
    Spinner spinnerMovieList;
    GeoPoint currPoint;
    XMLFunctions objXml;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maptag);
        
        pointsList = new ArrayList<GeoPoint>();
        mapView = (MapView) findViewById(R.id.map_tagview);       
        mapView.setBuiltInZoomControls(true);
        
        final List<Overlay> mapOverlays = mapView.getOverlays();
        final Drawable curLocDrawable = this.getResources().getDrawable(R.drawable.currlocation);
        final CustomItemizedOverlay curLocOverlay = 
                new CustomItemizedOverlay(curLocDrawable, this);
        
        
        final MapController controller = mapView.getController();
        OverlayItem overlayitem;
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        if (locationManager == null) {
            Toast.makeText(SFMapTagActivity.this,
                    "Location Manager Not Available", Toast.LENGTH_SHORT)
                    .show();
            return;
        }
        location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null)
            location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            currPoint = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
            overlayitem = 
                    new OverlayItem(currPoint, "You are here!", "");
               
               curLocOverlay.addOverlay(overlayitem);
               mapOverlays.add(curLocOverlay);
               controller.animateTo(currPoint);
               controller.setZoom(15);
        }
        
        objXml= new XMLFunctions();
        
        ArrayList<String> movieList = objXml.getAllMovies(this);
        //Load the movie list in the spinner
        spinnerMovieList = (Spinner) findViewById(R.id.spinner_movielist);
        //spinnerMovie.setOnItemSelectedListener(new MovieOnItemSelectedListener());        
        ArrayAdapter<String> adapterMovieList = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, movieList);
        
        adapterMovieList.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerMovieList.setAdapter(adapterMovieList);      
              
        
        
    }

    
    public void launchAR(View v) {
    	Intent mapIntent = new Intent(getApplicationContext(), CameraActivity.class);
        startActivity(mapIntent);
    
    }
    
    public void saveTag(View v) {
    	EditText time = (EditText) findViewById(R.id.txt_time);
    	EditText desc = (EditText) findViewById(R.id.txt_description);
    	String title = spinnerMovieList.getSelectedItem().toString();
    	movieToAdd = new Movie(title);
    	LocationData locToAdd =new LocationData(Double.toString(location.getLatitude()),Double.toString(location.getLongitude()));
				locToAdd.setDescription(desc.getText().toString());
				locToAdd.setTime(time.getText().toString());
    	movieToAdd.addLocation(locToAdd);
    	boolean result =objXml.addMovieTag(getApplicationContext(),movieToAdd);
    	if(result) {

        	Toast.makeText(getApplicationContext(),"Location Tagged!",Toast.LENGTH_SHORT).show();
    	}else {

        	Toast.makeText(getApplicationContext(),"Oops! Tag failed. Try Again.",Toast.LENGTH_SHORT).show();
    	}
    }
    
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    
    
    
        
    
}

